package com.kelvin.oocl.crm.service.impl;

import com.kelvin.oocl.crm.ApplicationInitializer;
import com.kelvin.oocl.crm.entity.Command;
import com.kelvin.oocl.crm.entity.Condition;
import com.kelvin.oocl.crm.entity.Customer;
import com.kelvin.oocl.crm.exception.ParameterBadFormatException;
import com.kelvin.oocl.crm.repository.ICustomerDao;
import com.kelvin.oocl.crm.service.CommandParser;
import com.kelvin.oocl.crm.util.FormatPrinter;
import com.kelvin.oocl.crm.util.MyLinkList;

public class CommandParserImpl implements CommandParser {
	private ICustomerDao customerDao = ApplicationInitializer.getApplicationContext().findBean("customerDao",ICustomerDao.class);
	
	@Override
	public MyLinkList<Customer> parseFind(Command command) throws ParameterBadFormatException {
		if(command.getParameters()[0].equals("noparameter")){
			return customerDao.findAll();
		}else {
			try {
				MyLinkList<Condition> conditions = new MyLinkList<>();
				Condition condition = null;
				boolean flag = false;
				int id = 0;
				String first = command.getParameters()[0];
				for (String pro : first.split(",")) {
					String[] str = pro.split(":");
					if("id".equals(str[0])){
						flag = true;
						id = Integer.parseInt(str[1]);
						break;
					}
					condition = new Condition();
					condition.setKey(str[0]);
					condition.setValue(str[1]);
					conditions.add(condition);
				}
				if(flag){
					MyLinkList<Customer> list = new MyLinkList<>();
					list.add(customerDao.findById(id));
					return list;
				}else {
					return customerDao.findByConditions(conditions);
				}
			} catch (NumberFormatException e) {
				throw new ParameterBadFormatException(FormatPrinter.badFormatMsg);
			}catch (ArrayIndexOutOfBoundsException e) {
				throw new ParameterBadFormatException(FormatPrinter.badFormatMsg);
			}
		}
	}

	@Override
	public boolean parseAdd(Command commond) throws ParameterBadFormatException {
		if(commond.getParameters()[0].equals("noparameter")){
			throw new ParameterBadFormatException(FormatPrinter.badFormatMsg);
		}else {
			String first = commond.getParameters()[0];
			Customer customer = this.parseCustomer(first.split(","));
			if(customer.getName()==null){
				throw new ParameterBadFormatException("You have to input the name when add a customer!");
			}
			return customerDao.add(customer);
		}
	}

	@Override
	public boolean parseDelete(Command commond) throws ParameterBadFormatException {
		if(commond.getParameters()[0].equals("noparameter")){
			throw new ParameterBadFormatException(FormatPrinter.badFormatMsg);
		}else {
			try {
				return customerDao.deleteById(Integer.parseInt(commond.getParameters()[0]));
			} catch (NumberFormatException e) {
				throw new ParameterBadFormatException(FormatPrinter.badFormatMsg);
			}
		}
	}

	@Override
	public boolean parseUpdate(Command commond) throws ParameterBadFormatException {
		if(commond.getParameters()[0].equals("noparameter")){
			throw new ParameterBadFormatException(FormatPrinter.badFormatMsg);
		}else {
			try {
				int id=Integer.parseInt(commond.getParameters()[0]);
				String first = commond.getParameters()[1];
				Customer customer = this.parseCustomer(first.split(","));
				if(null==customer){
					return false;
				}
				customer.setId(id);
				return customerDao.updateById(customer);
			} catch (ArrayIndexOutOfBoundsException e) {
				throw new ParameterBadFormatException(FormatPrinter.badFormatMsg);
			}catch (NumberFormatException e) {
				throw new ParameterBadFormatException(FormatPrinter.badFormatMsg);
			}
		}
	}

	@Override
	public MyLinkList<Customer> parseSort(Command commond) throws ParameterBadFormatException {
		if(commond.getParameters()[0].equals("noparameter") || commond.getParameters()[0].equals("id")){
			return this.customerDao.sortById();
		}else if(commond.getParameters()[0].equals("name")){
			return this.customerDao.sortByName();
		}else {
			throw new ParameterBadFormatException("Can not sort by "+commond.getParameters()[0]);
		}
	}

	//解析属性字符串
	public Customer parseCustomer(String[] pros) throws ParameterBadFormatException {
		if(null==pros || pros.length==0){
			return null;
		}
		try {
			Customer customer = new Customer();
			for (String pro : pros) {
				String[] str = pro.split(":");
				if(str[0].equals("name")){
					customer.setName(str[1]);
				}else if(str[0].equals("sex")){
					customer.setSex(str[1]);
				}else if(str[0].equals("address")){
					customer.setAddress(str[1]);
				}else if(str[0].equals("birthDate")){
					customer.setBirthDate(str[1]);
				}else if(str[0].equals("phoneNumber")){
					customer.setPhoneNumber(str[1]);
				}
			}
			return customer;
		} catch (ArrayIndexOutOfBoundsException e) {
			throw new ParameterBadFormatException(FormatPrinter.badFormatMsg);
		}
	}

}
